<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { getStatusAll } from '../api/coupon.js';
import { saveMyCoupon } from '../api/mycoupon.js';

let couponList = ref([]);

onMounted(() => {
  getMyCoupon();
});

async function getMyCoupon() {
  const response = await getStatusAll();
  console.log(response);
  couponList.value = response.data.data;
}

async function addMyCoupon(couponVO) {
  const mycoupon = { couId: couponVO.coupon.couId };
  console.log("要领取的优惠券是：" + mycoupon);
        
  const response = await saveMyCoupon(mycoupon);
  console.log(response);
  if (response.data.code === 200) {
    couponVO.mycoupon = { received: true };  // 前端样式 灰色的 已领取按钮
  }
}
</script>

<template>
  <div class="container">
    <div class="header">
      <div class="title">平台优惠券</div>
      <div class="sub">精选可领取优惠券</div>
    </div>

    <div class="coupon-grid">
      <div v-for="vo in couponList" :key="vo.coupon.couId" class="card">
        <div class="tag discount">
          <div class="price">¥{{ vo.coupon.couMoney }}</div>
          <div class="unit">{{ vo.coupon.couType }}</div>
        </div>
        
        <div class="info">
          <div class="name">{{ vo.coupon.couName }}</div>
          <div class="meta">
            <span>有效期： {{ vo.coupon.couStartTime }} 至 {{ vo.coupon.couEndTime }}</span>
            <span>发布时间： {{ vo.coupon.couPublishTime }}</span>
            <span>类型：{{ vo.coupon.couType }}</span>
          </div>
        </div>

        <div class="right">
          <button 
            class="btn"
            :class="vo.mycoupon ? 'disabled' : 'receive'"
            :disabled="vo.mycoupon"
            @click="!vo.mycoupon && addMyCoupon(vo)">
            {{ vo.mycoupon ? '已领取' : '领取' }}
          </button> 
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
/* 外部容器设置 */
.container {
  padding-top: 100px; /* 上边距 100px */
  padding-left: 20px;
  padding-right: 20px;
  background-color: #f9f9f9;
}

.header {
  text-align: center;
  margin-bottom: 20px;
}

.title {
  font-size: 28px;
  font-weight: bold;
  color: #333;
}

.sub {
  font-size: 16px;
  color: #888;
}

/* 优惠券网格 */
.coupon-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr); /* 每行显示4个 */
  gap: 20px;
  justify-items: center;
  margin-top: 20px;
}

/* 每张优惠券卡片 */
.card {
  background-color: white;
  width: 100%;
  max-width: 250px; /* 限制每张卡片的最大宽度 */
  border-radius: 10px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  padding: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
}

/* 优惠券标签样式 */
.tag {
  background-color: #f1f1f1;
  border-radius: 8px;
  padding: 10px;
  margin-bottom: 10px;
  width: 100%;
}

.discount .price {
  font-size: 24px;
  font-weight: bold;
  color: #f10180; /* 粉红色 */
}

.discount .unit {
  font-size: 14px;
  color: #888;
}

/* 优惠券信息部分 */
.info {
  margin-bottom: 20px;
}

.name {
  font-size: 18px;
  font-weight: 500;
  color: #333;
  margin-bottom: 10px;
}

.meta {
  font-size: 14px;
  color: #777;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

/* 按钮区域 */
.right {
  width: 100%;
}

.btn {
  width: 100%;
  padding: 10px;
  border: none;
  border-radius: 5px;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.btn.receive {
  background-color: #f10180; /* 粉红色 */
  color: white;
}

.btn.receive:hover {
  background-color: #d40070; /* 更深的粉红色 */
}

.btn.disabled {
  background-color: #ccc;
  color: #666;
  cursor: not-allowed;
}

/* 响应式调整 */
@media (max-width: 1024px) {
  .coupon-grid {
    grid-template-columns: repeat(3, 1fr); /* 大屏幕上改为每行3个 */
  }
}

@media (max-width: 768px) {
  .coupon-grid {
    grid-template-columns: repeat(2, 1fr); /* 小屏幕上改为每行2个 */
  }
}

@media (max-width: 480px) {
  .coupon-grid {
    grid-template-columns: 1fr; /* 超小屏幕上改为每行1个 */
  }
}
</style>
